package com.tencent.cloud.iov.recycler.block;

import android.support.v7.util.DiffUtil;

import java.util.List;

/**
 * 列表差异（部分）更新工具类
 *
 * {@link T} 子类重写 {@link #equals(Object)} 方法，实现列表数据比较，以优化列表更新速度
 *
 * @author showxiao
 * @date 2018/4/2
 */

public class DiffCallback<T> extends DiffUtil.Callback {

    private List<T> mOldItems;
    private List<T> mNewItems;

    public DiffCallback(List<T> oldItems, List<T> newItems) {
        mOldItems = oldItems;
        mNewItems = newItems;
    }

    @Override
    public int getOldListSize() {
        return mOldItems != null ? mOldItems.size() : 0;
    }

    @Override
    public int getNewListSize() {
        return mNewItems != null ? mNewItems.size() : 0;
    }

    @Override
    public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
        return mOldItems.get(oldItemPosition) != null
                && mNewItems.get(newItemPosition) != null
                && (mOldItems.get(oldItemPosition).getClass()
                == mNewItems.get(newItemPosition).getClass());
    }

    @Override
    public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
        return mOldItems.get(oldItemPosition).equals(mNewItems.get(newItemPosition));
    }
}
